package com.xk.demo.role.Controller;

import com.xk.demo.common.control.BaseController;
import com.xk.demo.mybatis.entity.PtRoleEntity;
import com.xk.demo.mybatis.flexigrid.PadingRstType;
import com.xk.demo.mybatis.flexigrid.PagingBean;
import com.xk.demo.mybatis.ztree.ZtreeEntity;
import com.xk.demo.role.entity.SearchRoleEntity;
import com.xk.demo.role.service.RoleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    private Logger logger =  LogManager.getLogger(this.getClass().getName());
    @Resource
    private RoleService roleService;
    @Secured("ROLE_gqgy_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/getRoleInfoListByPage")//分页
    @ResponseBody
    public PadingRstType<PtRoleEntity> getRoleInfoListByPage(SearchRoleEntity search, PagingBean paging){
        logger.info(search);
        PadingRstType<PtRoleEntity> ptUserEntityPadingRstType = roleService.getRoleInfoListByPage(search,paging);
        return ptUserEntityPadingRstType;
    }

    @RequestMapping("/getMenuZtreeListByRoleId")
    @ResponseBody
    public List<ZtreeEntity> getMenuZtreeListByRoleId(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid"+roleUuid);
        List<ZtreeEntity> list=roleService.getMenuZtreeListByRoleId(roleUuid);
        return list;

    }

    @RequestMapping("/saveMenuByRoleId")
    @ResponseBody
    public String saveMenuByRoleId(@RequestParam("roleUuid") String roleUuid,@RequestParam("menuIds") String menuIds){
        logger.info("roleUuid:"+roleUuid+" menuids:"+menuIds);
        roleService.saveMenuByRoleId(roleUuid,menuIds);
        return getSuccessMessag("role.menu.allocate.success");

    }

    @RequestMapping("/addBtn")
    @ResponseBody
    public void addBtn(@RequestParam("roleName") String roleName){
        PtRoleEntity ptRoleEntity = new PtRoleEntity();
        ptRoleEntity.setRoleName(roleName);
        roleService.insertRole(ptRoleEntity);
    }
    @RequestMapping("/delBtn")
    @ResponseBody
    public String delBtn(@RequestParam("uuid") String uuid){
        int flag=roleService.delRole(uuid);
        return flag+"";
    }
}

